package com.loklok.confuse.ui;

import com.intellij.ide.impl.ProjectUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.loklok.confuse.extension.PathConfigSettingCache;
import com.loklok.confuse.model.CodePathConfig;
import com.loklok.confuse.util.CommonUtil;
import com.loklok.confuse.util.FilePackageChooseUtils;
import com.loklok.confuse.util.LocalFileUtils;
import com.loklok.confuse.util.VelocityUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GenerateDtoVoCodeDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField classPrefixText;
    private JRadioButton voCodeRadio;
    private JRadioButton dtoCodeRadio;
    private JTextField vmCodeText;
    private JButton chooseVmBut;

    public GenerateDtoVoCodeDialog(AnActionEvent anActionEvent) {
        setContentPane(contentPane);
        setTitle("codeConfuse");
        setSize(800, 300);
        setLocation(600, 200);
        setModal(true);
        setResizable(true);
        getRootPane().setDefaultButton(buttonOK);


        PathConfigSettingCache state = PathConfigSettingCache.getInstance(anActionEvent.getProject());
        Project project = anActionEvent.getProject();
        if (state == null || state.getConfig() == null || state.getConfig().requiredFieldsEmpty()) {
            CommonUtil.configureMessageReminders(project);
            return;

        }
        CodePathConfig config = state.getConfig();

        chooseVmBut.addActionListener(actionEvent -> FilePackageChooseUtils.chooseFileAndSetPath(project, vmCodeText));

        buttonOK.addActionListener(e -> {

            if(StringUtils.isEmpty(classPrefixText.getText()) ||
                    !(voCodeRadio.isSelected() || dtoCodeRadio.isSelected()) || StringUtils.isEmpty(vmCodeText.getText())){
                Messages.showInfoMessage("类前缀和选择类型以及生成模板都是必填!", "Error");
                return;
            }
            ProgressManager.getInstance().run(new Task.Backgroundable(project, "Generating Files", false) {
                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    indicator.setText("ConfuseCode plugin Generating files...");
                    indicator.setFraction(0.0); // 初始化进度
                    boolean isChooseVo = voCodeRadio.isSelected();
                    VelocityUtils.generateDtoVoCode(anActionEvent, project, config, vmCodeText.getText(), classPrefixText.getText(), isChooseVo);

                    indicator.setFraction(1.0); // 完成进度
                    indicator.setText("ConfuseCode plugin File generation complete!");
                }
            });
            onOK();
        });
    }

    private void onOK() {
        // add your code here
        dispose();
    }
}
